/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2023-03-15 10:54:32
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2023-03-16 15:38:53
 */
'use static'
process.setMaxListeners(0)
const Koa = require('koa')
const bodyParser = require('koa-bodyparser');
const cors = require('koa2-cors')
const helmet = require("koa-helmet")


const corsConfig = require('./src/middlewares/cors')
const {logInfo} = require('./src/middlewares/logger.js')
const {error,success} = require('./src/middlewares/response')

const {
    pubicRouter,
    privateRouter
} = require('./src/router/index')


const app = new Koa();
console.log('启动')
app.use((ctx, next) => {
    const start = new Date();
    console.log(1);
    next();
    const delta = new Date() - start;
    console.log('请求时间',delta);
    console.log(1);
})
app.use(async (ctx, next) => {
    console.log(2);
    // await global.fetch .get('http://baidu.com')
    next();
    console.log(2);
})
app.use((ctx, next) => {
    console.log(3);
    next();
    console.log(3);
})
// 输出顺序：123-321体现了中间件迭代模型
app.use(logInfo);//日志管理
app.use(error)//统一异常处理
app.use(bodyParser())
app.use(helmet())//网络安全中间件

app.use(cors(corsConfig))//cor管理

app.use(pubicRouter.routes(), pubicRouter.allowedMethods()) // 公共路由
app.use(privateRouter.routes(), privateRouter.allowedMethods()) // 私有路由

app.use(success)//response认证响应
app.use((ctx)=>{
    //ctx.body='hello,koa'
})


module.exports=app;
